package com.scpii.universal.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.scpii.universal.hibernate.dao.BaseDao;
import com.scpii.universal.hibernate.entity.ClientUserTab;
import com.scpii.universal.hibernate.entity.ClientUserTab;
import com.scpii.universal.hibernate.intface.BaseInterface;
import com.scpii.universal.util.SpringBeanFactory;

public class ClientUserTabService implements BaseInterface<ClientUserTab> {
	private static final Logger log = LoggerFactory
			.getLogger(ClientUserTabService.class);
	private BaseDao<ClientUserTab> clientUserTabDao = SpringBeanFactory.clientUserTabDao;
//	private BaseDao<ClientUserTab> clientUserTabDao;
	
	public ClientUserTab get(Integer id) throws Exception {
		return clientUserTabDao.get(id);
	}
	
	public ClientUserTab get(Long id) throws Exception {
		return clientUserTabDao.get(id);
	}

	public List<ClientUserTab> getAll() throws Exception {
		return clientUserTabDao.getAll();
	}

	public ClientUserTab get(String propertyName, Object value) throws Exception {
		return null;
	}

	public List<ClientUserTab> getList(String propertyName, Object value)
			throws Exception {
		return null;
	}

	public Long getTotalCount() throws Exception {
		return null;
	}

	public void save(ClientUserTab obj) throws Exception {
		clientUserTabDao.save(obj);
	}

	public void update(ClientUserTab obj) throws Exception {

	}

	public void delete(ClientUserTab obj) throws Exception {

	}

	public void delete(Integer id) throws Exception {

	}

	public List<ClientUserTab> hqlGetList(String hql, Map<String, Object> argsMap)
			throws Exception {
		return clientUserTabDao.hqlGetList(hql, argsMap);
	}

	public void hqlUpdate(String namedHqlQuery, Map<String, Object> argsMap)
			throws Exception {
		clientUserTabDao.hqlUpdate(namedHqlQuery, argsMap);
	}

	public Long hqlGetNums(String namedHqlQuery, Map<String, Object> argsMap)
			throws Exception {
		return clientUserTabDao.hqlGetNums(namedHqlQuery, argsMap);
	}

	public List<ClientUserTab> sqlGetList(String sqls, Map<String, Object> argsMap)
			throws Exception {
		return clientUserTabDao.sqlGetList(sqls, argsMap);
	}

	public void sqlUpdate(String sqls, Map<String, Object> argsMap)
			throws Exception {
		clientUserTabDao.sqlUpdate(sqls, argsMap);
	}

	public Long sqlGetNum(String sqls, Map<String, Object> argsMap)
			throws Exception {
		return clientUserTabDao.sqlGetNum(sqls, argsMap);
	}
	
	/**
	 * 客户端用户登录验证
	 * @param userName
	 * @param userPassword
	 * @return
	 * @throws Exception
	 * @author 甘建新
	 * 2012-7-19下午11:04:34
	 */
	public ClientUserTab clientUserLogingCheck(String userName,String userPassword)throws Exception{
		Map<String, Object> argsMap = new HashMap<String, Object>();
		argsMap.put("userName", userName);
		argsMap.put("userPassword", userPassword);
		
		List<ClientUserTab> userList = this.hqlGetList("CLIENTUSER_LOGIN_CHECK", argsMap);
		if (userList!=null && userList.size()==1) {
			return userList.get(0);
		}
		return null;
	}
	
	/**
	 * 获得相同用户名和密码的个数
	 * @param userName
	 * @param userPassword
	 * @return
	 * @throws Exception
	 * @author 甘建新
	 * 2012-7-24上午02:40:16
	 */
	public Long clientUserNum(String userName,String userPassword,String userPhoneNum)throws Exception{
		Map<String, Object> argsMap = new HashMap<String, Object>();
		argsMap.put("userName", userName);
		argsMap.put("userPassword", userPassword);
		argsMap.put("phoneNum", userPhoneNum);
		
		long num = this.hqlGetNums("CLIENTUSER_NUM", argsMap);
		return num;
	}
	
	/**
	 * 客户端用户验证
	 * @param userName
	 * @param userPhoneNum
	 * @return
	 * @throws Exception
	 * @author 甘建新
	 * 2012-8-1下午02:18:29
	 */
	public ClientUserTab checkUserInfo(String userName,String userPhoneNum,String varDeviceId,String varImsi,int osType,int appId)throws Exception{
		Map<String, Object> argsMap = new HashMap<String, Object>();
		argsMap.put("userPhoneNum", userPhoneNum);
		//首先需要验证用户手机号码是否存在
		List<ClientUserTab> userList = this.hqlGetList("CHECK_USER_PHONEUNM", argsMap);
		//存在返回用户信息
		if (userList!=null && userList.size()>0) {
			ClientUserTab userTab = userList.get(0);
			Long userId = userTab.getIntUserId();
			//更新用户信息
			argsMap.clear();//清空Map
			argsMap.put("userName", userName);
			argsMap.put("userPhoneNum", userPhoneNum);
			argsMap.put("userId", userId);
			argsMap.put("updateTime", userTab.getDatUpdateDatetime());
			
			this.hqlUpdate("UPDATE_USER_INFO", argsMap);
			//获得用户信息
			return this.get(userId);
		}else {
			//新增用户信息
			ClientUserTab userTab = new ClientUserTab();
			userTab.setVarUserLoginName(userName);
			userTab.setVarUserPhonenum(userPhoneNum);
			userTab.setVarDeviceId(varDeviceId);
			userTab.setVarImsi(varImsi);
			userTab.setIntOsType(osType);
			userTab.setIntAppId(appId);
			//保存用户信息
			this.save(userTab);
			//获取用户信息
			userList =  this.hqlGetList("CHECK_USER_PHONEUNM", argsMap);
			return (userList!=null && userList.size()>0)?userList.get(0):null;
		}
	}

	//Setter
	public void setClientUserTabDao(BaseDao<ClientUserTab> clientUserTabDao) {
		this.clientUserTabDao = clientUserTabDao;
	}
	
	public static void main(String[] args) {
		ClientUserTabService clientUserService = new ClientUserTabService();
		try {
			ClientUserTab userTab = clientUserService.checkUserInfo("aaaa", "aaaa", "bbbbb", "ddddd", 0, 1);
			System.out.println(userTab.getIntUserId());
			System.out.println(userTab.getVarDeviceId());
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
}